# 生产者
import json
import string
import time

from kafka import KafkaProducer, TopicPartition
from kafka.errors import KafkaError
import uuid
import random

randomStr = ''.join(random.choices(string.ascii_uppercase, k=8)).upper()

if __name__ == "__main__":
    producer = KafkaProducer(bootstrap_servers=["192.168.120.220:9093"])
    partition = TopicPartition('mytopic', 1)

    while True:
        # for i in range(10):
        #     print(i + 1)
        print("\n------------------------------------------------------------------------\n")
        with open('./智慧提醒.json', 'r', encoding='utf8') as json_f:

            sendData = json.loads(json_f.read())
        uuid32 = uuid.uuid4().hex
        sendData["id"] = uuid32
        print(sendData)

        message = "Message:  hello {}".format(random.randint(a=1, b=1000))
        future = producer.send("SYSTEM_MASTER_IN", bytes(json.dumps(sendData), "utf-8"), partition=1)
        try:
            record_metadata = future.get(timeout=10)
            print(
                "Message  sent to partition {} with offset {} {}".format(
                    # sendData,
                    record_metadata.partition, record_metadata.offset, record_metadata.partition
                )
            )
        except KafkaError as e:
            print("Failed to send message {}: {}".format(sendData, e))
        time.sleep(0.2)
